package com.cpi.cornpp.purchase.program.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseOrderChargeForm;
import com.cpi.cornpp.purchase.program.services.PurchaseOrderChargeService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderChargeView;

public class PurchaseOrderChargeAction extends MappingDispatchAction {
	private static final String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String PURCHASE_ORDER_ID = "purchaseOrderId";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String COST_ELEMENT_ID = "costElementId";//$NON-NLS-1$
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private static final String COST_ELEMENT_LIST = "costElementsList";//$NON-NLS-1$
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		CostElementServices costElementServices = null;
		HttpSession httpSession = null;
		PurchaseOrderChargeForm purchaseOrderChargeForm = null;
		String purchaseOrderId = null;
		List costElementViews = null;
		try{
			costElementServices = new CostElementServices();
			costElementViews = (List)costElementServices.getCostElementList();
			httpSession = request.getSession();
			purchaseOrderId = request.getParameter(PURCHASE_ORDER_ID);
			purchaseOrderChargeForm = (PurchaseOrderChargeForm)form;
			if( purchaseOrderChargeForm.getPurchaseOrderChargeView() == null ){
				purchaseOrderChargeForm.setPurchaseOrderChargeView(new PurchaseOrderChargeView());
			}
			purchaseOrderChargeForm.getPurchaseOrderChargeView().setPurchaseOrderChargeTypeId(purchaseOrderId);
			httpSession.setAttribute(PURCHASE_ORDER_ID, purchaseOrderId);
			request.setAttribute(COST_ELEMENT_LIST, costElementViews);
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseOrderChargeService purchaseOrderChargeService = null;
		PurchaseOrderChargeForm purchaseOrderChargeForm = null;
		String messageError = null;
		MessageResources messageResources = null;
		String forward = DEFAULT_FORWARD;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			purchaseOrderChargeService = new PurchaseOrderChargeService();
			purchaseOrderChargeForm = (PurchaseOrderChargeForm)form;
			purchaseOrderChargeService.insert(purchaseOrderChargeForm.getPurchaseOrderChargeView());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (PurchaseProgramException purchaseProgramException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), purchaseProgramException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
				form.reset(mapping, request);
				throw purchaseProgramException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		String purchaseOrderId = null;
		String costElementId = null;
		PurchaseOrderChargeService purchaseOrderChargeService = null;
		PurchaseOrderChargeView purchaseOrderChargeView = null;
		String messageError = null;
		MessageResources messageResources = null;
		try{
			messageResources = getResources(request);
			purchaseOrderChargeView = new PurchaseOrderChargeView();
			purchaseOrderChargeService = new PurchaseOrderChargeService();
			httpSession = request.getSession();
			purchaseOrderId = request.getParameter(PURCHASE_ORDER_ID);
			costElementId = request.getParameter(COST_ELEMENT_ID);
			purchaseOrderChargeView.setPurchaseOrderId(purchaseOrderId);
			purchaseOrderChargeView.setPurchaseOrderChargeTypeId(costElementId);
			purchaseOrderChargeService.delete(purchaseOrderChargeView);
			httpSession.setAttribute(PURCHASE_ORDER_ID, purchaseOrderId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (PurchaseProgramException purchaseProgramException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
}